package com.limboline.bssystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.limboline.bssystem.entity.CommodityEntity;
import com.limboline.bssystem.entity.CommodityVO;
import com.limboline.bssystem.entity.CustomerEntity;
import com.limboline.bssystem.entity.CustomerVO;
import com.limboline.bssystem.mapper.CommodityEntityMapper;
import com.limboline.bssystem.mapper.CustomerEntityMapper;
import com.limboline.bssystem.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired(required = false)
    CustomerEntityMapper customerEntityMapper;

    @Autowired(required = false)
    CommodityEntityMapper commodityEntityMapper;

    @Override
    public void listOrder(ModelAndView modelAndView) {
        List<CustomerEntity> customerEntities = customerEntityMapper.selectByState(0);
        List<CustomerVO> customerVOS = new ArrayList<>();
        for(CustomerEntity customerEntity : customerEntities){
            CustomerVO customerVO = new CustomerVO();
            BeanUtils.copyProperties(customerEntity, customerVO);
            CommodityEntity commodityEntity = commodityEntityMapper.selectByPrimaryKey(customerEntity.getCid());
            customerVO.setPrice(commodityEntity.getPrice());
            customerVO.setTitle(commodityEntity.getTitle());
            customerVO.setPicUrl(commodityEntity.getPicUrl());
            customerVOS.add(customerVO);
        }
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(customerVOS));
        modelAndView.addObject("record", array.toString());
    }

    @Override
    public boolean buy(String data) {
        JSONArray array = JSON.parseArray(data);
        for(int i = 0;i < array.size();i++){
            Long datetime = new Date().getTime();
            JSONObject json = array.getJSONObject(i);

            CommodityEntity commodityEntity = commodityEntityMapper.selectByPrimaryKey(json.getInteger("cid"));
            commodityEntity.setPurchaseDate(datetime);
            commodityEntity.setNum(json.getInteger("number"));
            commodityEntityMapper.updateByPrimaryKey(commodityEntity);

            CustomerEntity customerEntity = customerEntityMapper.selectByCid(json.getInteger("cid"));
            customerEntity.setNum(json.getInteger("number"));
            customerEntity.setState(1);
            customerEntity.setPurchaseDate(datetime);
            customerEntity.setPrice(commodityEntity.getPrice());
            if(customerEntityMapper.updateByPrimaryKeySelective(customerEntity) == 0){
                return false;
            }

        }
        return true;
    }

    @Override
    public void listBill(ModelAndView modelAndView) {

        List<CustomerEntity> customerEntities = customerEntityMapper.selectByState(1);
        double totalValue = 0.0;
        List<CommodityVO> commodityVOS = new ArrayList<>();
        for(CustomerEntity entity : customerEntities){
            CommodityEntity commodityEntity = commodityEntityMapper.selectByPrimaryKey(entity.getCid());
            CommodityVO commodityVO = new CommodityVO();
            BeanUtils.copyProperties(commodityEntity, commodityVO);
            commodityVO.setPrice(entity.getPrice());
            commodityVOS.add(commodityVO);
            totalValue += entity.getPrice()*entity.getNum();
        }

        JSONArray arr = JSON.parseArray(JSON.toJSONString(commodityVOS));
        modelAndView.addObject("arr", arr);
        modelAndView.addObject("totalValue", totalValue);
    }

    @Override
    public void addOrder(Integer id, Integer num) {
        CustomerEntity customerEntity = customerEntityMapper.selectByCid(id);
        if(customerEntity == null){
            customerEntity = new CustomerEntity();
            customerEntity.setState(0);
            customerEntity.setCid(id);
            customerEntity.setNum(num);
            customerEntityMapper.insertSelective(customerEntity);
        }else{
            customerEntity.setNum(customerEntity.getNum() + num);
            customerEntityMapper.updateByPrimaryKeySelective(customerEntity);
        }

    }
}
